package gameObject
{
	import assets.Asset;
	
	import away3d.bounds.NullBounds;
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Sprite3D;
	import away3d.events.MouseEvent3D;
	import away3d.materials.ColorMaterial;
	
	import events.LoadedEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import flash.utils.Dictionary;
	
	import game.BattleWorld;
	import game.StoreHouse;
	
	import gameObject.resources3D.GameMesh;
	import gameObject.resources3D.MapResource;
	
	import tool.BitmapTool;

	public class CitadelMap extends MapBase
	{
		private var _cameraSetting:String = Global.iniAddress+"Citadel_Camera.xml";
		private var _txtSettingURL:String = Global.assetPath+"UI/citadelTxt.xml";
		private var _txtXML:XML;
		private var _txtURL:String;
		private var settingLoader:URLLoader;
		
		private var _texts:Dictionary;
		private var selector:Dictionary;
		
		private var activeTxt:Bitmap;
		private var _txtHolders:Dictionary = new Dictionary;
		
		public function CitadelMap()
		{
			_cameraRoot = new ObjectContainer3D;
			addChild(_cameraRoot);
			
			settingLoader = new URLLoader;//取相机
			settingLoader.addEventListener(Event.COMPLETE,cameraLoaded);
			settingLoader.load(new URLRequest(_cameraSetting));
		}

		private function cameraLoaded(e:Event):void
		{
			settingLoader.removeEventListener(Event.COMPLETE,cameraLoaded);
			
			var xml:XML = new XML(settingLoader.data);
			Global.gameView.getCamera(xml);
			_cameraRoot.addChild(Global.gameView.camera);
			
			settingLoader.addEventListener(Event.COMPLETE,txtXMLLoaded);
			settingLoader.load(new URLRequest(_txtSettingURL));
			
		}
		private function txtXMLLoaded(e:Event):void
		{
			settingLoader.removeEventListener(Event.COMPLETE,txtXMLLoaded);
			_txtXML = new XML(settingLoader.data);
			
			_txtURL = Global.assetPath+_txtXML.@ima;
			
			Asset.bitmapdata.addEventListener(LoadedEvent.BITMAPDATA_COMPLETE,txtBmpLoaded);
			Asset.bitmapdata.load(_txtURL);
		}
		
		private function txtBmpLoaded(e:LoadedEvent):void
		{
			if(e.URL==_txtURL)
			{
				Asset.bitmapdata.removeEventListener(LoadedEvent.BITMAPDATA_COMPLETE,txtBmpLoaded);
				
				var bmd:BitmapData = Asset.resBitmapDatas[_txtURL];
				_texts = BitmapTool.cutImageUseXML(bmd,_txtXML);
				
				_resource = new MapResource(0);//取出地图3D资源
				_resource.addEventListener(Event.COMPLETE,gotResource);
				_resource.load();
			}
			
		}
		private function gotResource(e:Event):void
		{
			var mapRes:MapResource = e.currentTarget as MapResource;
			mapRes.removeEventListener(Event.COMPLETE,gotResource);
			addChild(mapRes);
			
			selector = new Dictionary;
			
			selector["tradingStation"] = getMeshByName("tradingStation");
			selector["stHouse"] = getMeshByName("stHouse");
			selector["institute"] = getMeshByName("institute");
			selector["arena"] = getMeshByName("arena");
			selector["army"] = getMeshByName("army");
			selector["shop"] = getMeshByName("shop");
			selector["headquarters"] = getMeshByName("headquarters");
			selector["outside"] = getMeshByName("outside");
			
			var gm:GameMesh;
			for (var key:String in selector)
			{
				gm = selector[key];
				gm.mouseEnabled = true;
				gm.addEventListener(MouseEvent3D.MOUSE_OVER,onMouseOver);
				gm.addEventListener(MouseEvent3D.MOUSE_OUT,onMouseOut);
				gm.addEventListener(MouseEvent3D.MOUSE_DOWN,onMouseDown);
				
				
				var bm:Bitmap = new Bitmap(_texts[key]);
				var sp:Sprite = new Sprite;
				sp.mouseEnabled = false;
				sp.addChild(bm);
				_txtHolders[key]=sp;
				
				Global.stage.addChild(sp);
			}
			
			activeTxt = new Bitmap(_texts["active"]);
			activeTxt.blendMode = BlendMode.SCREEN;
			
			dispatchEvent(new Event(Event.COMPLETE));
			
			Global.stage.addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		
		private function getMeshByName(name:String):GameMesh
		{
			var mapRes:MapResource = _resource as MapResource;
			
			for each(var gm:GameMesh in mapRes.meshes)
				if(gm.name==name)return gm;
				
			return null;
		}
		
		private function onEnterFrame(e:Event):void
		{
			var gm:GameMesh;
			for (var key:String in selector)
			{
				gm = selector[key];
				var bm:BitmapData = _texts[key];
				var sp:Sprite = _txtHolders[key];
				
				var screenPos:Vector3D = Global.gameView.convertToScreenPos(gm.scenePosition);
				
				sp.x = screenPos.x-bm.width*0.5;
				sp.y = screenPos.y-bm.height*0.5;
			}
		}
		
		private function onMouseOver(e:MouseEvent3D):void
		{
			var gm:GameMesh = e.currentTarget as GameMesh;
			var bm:BitmapData = _texts[gm.name];
			var sp:Sprite = _txtHolders[gm.name];
			
			sp.addChild(activeTxt);
			activeTxt.x = bm.width*0.5 - activeTxt.bitmapData.width*0.5;
			activeTxt.y = bm.height*0.5 - activeTxt.bitmapData.height*0.5;
			
			Mouse.cursor=MouseCursor.BUTTON;
			//trace(gm.name);
		}
		
		private function onMouseOut(e:MouseEvent3D):void
		{
			if(activeTxt.parent)activeTxt.parent.removeChild(activeTxt);
			
			Mouse.cursor=MouseCursor.ARROW;
		}
		
		private function onMouseDown(e:MouseEvent3D):void
		{
			var gm:GameMesh = e.currentTarget as GameMesh;
			gm.addEventListener(MouseEvent3D.MOUSE_UP,onMouseUp);
		}
		
		private function onMouseUp(e:MouseEvent3D):void
		{
			var gm:GameMesh = e.currentTarget as GameMesh;
			gm.removeEventListener(MouseEvent3D.MOUSE_UP,onMouseUp);
			
			switch(gm.name)
			{
				case "stHouse":
					clear();
					Global.stage.addChild(new StoreHouse);
					break;
				case "outside":
					clear();
					Global.stage.addChild(new BattleWorld(2,1,[Global.uid]));
					break;
				
				default:
					break;
			}
			
			
		}
		
		private function clear():void
		{
			if(parent)parent.removeChild(this);
			Global.stage.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			
			var gm:GameMesh;
			for (var key:String in selector)
			{
				gm = selector[key];
				gm.mouseEnabled = false;
				gm.removeEventListener(MouseEvent3D.MOUSE_OVER,onMouseOver);
				gm.removeEventListener(MouseEvent3D.MOUSE_OUT,onMouseOut);
				gm.removeEventListener(MouseEvent3D.MOUSE_UP,onMouseUp);
				
				var tx:Sprite = _txtHolders[key];
				if(tx.parent)tx.parent.removeChild(tx);
			}
			
			if(activeTxt.parent)activeTxt.parent.removeChild(activeTxt);
			
			Mouse.cursor=MouseCursor.ARROW;
			dispatchEvent(new Event(Event.CLOSE));
		}
		
	}
}